package view.manager;

import java.awt.EventQueue;

import javax.swing.JInternalFrame;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;

import base.NumberGeneration;
import dao.DepartmentDao;
import dao.MajorDao;
import dao.ManagerDao;
import model.Department;
import model.Major;
import model.Manager;
import util.StringUtil;

import javax.swing.JComboBox;
import javax.swing.JRadioButton;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.ImageIcon;
import javax.swing.JPasswordField;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.awt.event.ActionEvent;

public class TeacherAddInterfrm extends JInternalFrame {
	private JTextField textField;
	private JTextField textField_1;
	private JTextField textField_2;
	private final ButtonGroup buttonGroup = new ButtonGroup();
	private JPasswordField passwordField;
	private JComboBox comboBox;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					TeacherAddInterfrm frame = new TeacherAddInterfrm();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public TeacherAddInterfrm() {
		setTitle("教师添加");
		setIconifiable(true);
		setClosable(true);
		setBounds(100, 100, 450, 363);

		JLabel lblNewLabel = new JLabel("教师编号：");
		lblNewLabel.setIcon(new ImageIcon(TeacherAddInterfrm.class.getResource("/images/num.png")));

		textField = new JTextField();
		textField.setEditable(false);
		textField.setColumns(10);

		JLabel lblNewLabel_1 = new JLabel("教师姓名：");
		lblNewLabel_1.setIcon(new ImageIcon(TeacherAddInterfrm.class.getResource("/images/username.png")));

		textField_1 = new JTextField();
		textField_1.setColumns(10);

		comboBox = new JComboBox();
		comboBox.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (comboBox.getSelectedItem() instanceof Major) {
					textField.setText(NumberGeneration.createTeacherNum((Major)comboBox.getSelectedItem()));
				}else {
					textField.setText("先选择上级学院");
				}
			}
		});

		JLabel lblNewLabel_2 = new JLabel("所属系部：");
		lblNewLabel_2.setIcon(new ImageIcon(TeacherAddInterfrm.class.getResource("/images/name.png")));

		JLabel lblNewLabel_3 = new JLabel("教师电话：");
		lblNewLabel_3.setIcon(new ImageIcon(TeacherAddInterfrm.class.getResource("/images/电 话.png")));

		textField_2 = new JTextField();
		textField_2.setColumns(10);

		JLabel lblNewLabel_4 = new JLabel("教师权限：");
		lblNewLabel_4.setIcon(new ImageIcon(TeacherAddInterfrm.class.getResource("/images/super.png")));

		JRadioButton rdbtnNewRadioButton = new JRadioButton("普通教师");
		buttonGroup.add(rdbtnNewRadioButton);
		rdbtnNewRadioButton.setSelected(true);

		JRadioButton rdbtnNewRadioButton_1 = new JRadioButton("管理员");
		buttonGroup.add(rdbtnNewRadioButton_1);

		JButton btnNewButton = new JButton("添加");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String t_id, t_name, t_tel, t_password, m_id = null, issuper = "0";
				t_id = textField.getText();
				t_name = textField_1.getText();
				t_tel = textField_2.getText();
				t_password = String.valueOf(passwordField.getPassword());
				if (rdbtnNewRadioButton_1.isSelected()) {
					issuper = "1";
				}
				if (comboBox.getSelectedItem() instanceof Major) {
					Major major = (Major) comboBox.getSelectedItem();
					m_id = major.getM_id();
				}
				if (StringUtil.isEmpty(t_id)) {
					JOptionPane.showMessageDialog(null, "请选择专业", "提示", JOptionPane.ERROR_MESSAGE);
					textField.requestFocus();
					return;
				}
//				if (!StringUtil.isNum(t_id)) {
//					JOptionPane.showMessageDialog(null, "教师编号需为数字", "提示", JOptionPane.ERROR_MESSAGE);
//					textField.requestFocus();
//					return;
//				}
//				if (t_id.length() >= 9) {
//					JOptionPane.showMessageDialog(null, "教师编号太长", "提示", JOptionPane.ERROR_MESSAGE);
//					textField.requestFocus();
//					textField.selectAll();
//					return;
//				}
				if (StringUtil.isEmpty(t_name)) {
					JOptionPane.showMessageDialog(null, "教师姓名不能为空", "提示", JOptionPane.ERROR_MESSAGE);
					textField_1.requestFocus();
					return;
				}
				if (t_name.length() >= 30) {
					JOptionPane.showMessageDialog(null, "教师姓名太长", "提示", JOptionPane.ERROR_MESSAGE);
					textField_1.requestFocus();
					textField_1.selectAll();
					return;
				}
				if (StringUtil.isEmpty(t_tel)) {
					JOptionPane.showMessageDialog(null, "教师电话不能为空", "提示", JOptionPane.ERROR_MESSAGE);
					textField_2.requestFocus();
					return;
				}
				if (!StringUtil.checkPhone(t_tel)) {
					JOptionPane.showMessageDialog(null, "教师电话格式错误", "提示", JOptionPane.ERROR_MESSAGE);
					textField_2.requestFocus();
					textField_2.selectAll();
					return;
				}
				if (StringUtil.isEmpty(t_password)) {
					JOptionPane.showMessageDialog(null, "密码不能为空", "提示", JOptionPane.ERROR_MESSAGE);
					passwordField.requestFocus();
					return;
				}
				if (!StringUtil.checkPasswordRule(t_password)) {
					JOptionPane.showMessageDialog(null, "密码长度大于8位小于30位，包含3种字符", "提示", JOptionPane.ERROR_MESSAGE);
					passwordField.requestFocus();
					passwordField.selectAll();
					return;
				}
				Manager m = new Manager(t_id, t_name, t_tel, t_password, m_id, issuper);
				if (ManagerDao.add(m)>0) {
					JOptionPane.showMessageDialog(null, "添加成功", "提示", JOptionPane.INFORMATION_MESSAGE);
					comboBox.setSelectedIndex(0);
					rdbtnNewRadioButton.setSelected(true);
					textField_1.setText("");
					textField_2.setText("");
					passwordField.setText("");
				}
			}
		});
		btnNewButton.setIcon(new ImageIcon(TeacherAddInterfrm.class.getResource("/images/add.png")));

		JButton btnNewButton_1 = new JButton("重置");
		btnNewButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				comboBox.setSelectedIndex(0);
				textField_1.setText("");
				textField_2.setText("");
				passwordField.setText("");
			}
		});
		btnNewButton_1.setIcon(new ImageIcon(TeacherAddInterfrm.class.getResource("/images/resets.png")));

		JLabel lblNewLabel_5 = new JLabel("登录密码：");
		lblNewLabel_5.setIcon(new ImageIcon(TeacherAddInterfrm.class.getResource("/images/密码.png")));

		passwordField = new JPasswordField();
		GroupLayout groupLayout = new GroupLayout(getContentPane());
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.TRAILING)
				.addGroup(groupLayout.createSequentialGroup()
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addGroup(groupLayout.createSequentialGroup()
							.addGap(107)
							.addComponent(btnNewButton)
							.addPreferredGap(ComponentPlacement.RELATED, 81, Short.MAX_VALUE)
							.addComponent(btnNewButton_1))
						.addGroup(groupLayout.createParallelGroup(Alignment.LEADING, false)
							.addGroup(groupLayout.createSequentialGroup()
								.addGap(103)
								.addComponent(lblNewLabel_2)
								.addPreferredGap(ComponentPlacement.RELATED)
								.addComponent(comboBox, GroupLayout.PREFERRED_SIZE, 158, GroupLayout.PREFERRED_SIZE))
							.addGroup(groupLayout.createSequentialGroup()
								.addGap(104)
								.addGroup(groupLayout.createParallelGroup(Alignment.TRAILING)
									.addComponent(lblNewLabel_3)
									.addComponent(lblNewLabel_1)
									.addComponent(lblNewLabel))
								.addPreferredGap(ComponentPlacement.RELATED)
								.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
									.addComponent(textField, GroupLayout.PREFERRED_SIZE, 158, GroupLayout.PREFERRED_SIZE)
									.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
										.addComponent(textField_1, GroupLayout.PREFERRED_SIZE, 158, GroupLayout.PREFERRED_SIZE)
										.addComponent(textField_2)))))
						.addGroup(groupLayout.createSequentialGroup()
							.addGap(104)
							.addGroup(groupLayout.createParallelGroup(Alignment.TRAILING)
								.addComponent(lblNewLabel_4)
								.addComponent(lblNewLabel_5))
							.addGroup(groupLayout.createParallelGroup(Alignment.TRAILING)
								.addGroup(groupLayout.createSequentialGroup()
									.addGap(1)
									.addComponent(rdbtnNewRadioButton)
									.addGap(11)
									.addComponent(rdbtnNewRadioButton_1)
									.addPreferredGap(ComponentPlacement.RELATED, 16, Short.MAX_VALUE))
								.addGroup(groupLayout.createSequentialGroup()
									.addPreferredGap(ComponentPlacement.RELATED)
									.addComponent(passwordField, GroupLayout.DEFAULT_SIZE, 158, Short.MAX_VALUE)))))
					.addGap(84))
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addContainerGap()
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblNewLabel_2)
						.addComponent(comboBox, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(24)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblNewLabel)
						.addComponent(textField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(18)
					.addGroup(groupLayout.createParallelGroup(Alignment.TRAILING)
						.addComponent(textField_1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(lblNewLabel_1))
					.addGap(18)
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addGroup(groupLayout.createSequentialGroup()
							.addGap(16)
							.addComponent(lblNewLabel_3))
						.addGroup(groupLayout.createSequentialGroup()
							.addGap(13)
							.addComponent(textField_2, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)))
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addGroup(groupLayout.createSequentialGroup()
							.addGap(15)
							.addComponent(lblNewLabel_4))
						.addGroup(groupLayout.createSequentialGroup()
							.addGap(12)
							.addComponent(rdbtnNewRadioButton))
						.addGroup(groupLayout.createSequentialGroup()
							.addGap(12)
							.addComponent(rdbtnNewRadioButton_1)))
					.addGap(13)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(passwordField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(lblNewLabel_5))
					.addGap(40)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(btnNewButton)
						.addComponent(btnNewButton_1))
					.addContainerGap(27, Short.MAX_VALUE))
		);
		getContentPane().setLayout(groupLayout);

		fillDepartmentType();
	}

	/**
	 * 初始化下拉框
	 */
	private void fillDepartmentType() {
		ArrayList<Major> arrayList = MajorDao.select();
		comboBox.addItem("请选择专业");
		for (Major major : arrayList) {
			comboBox.addItem(major);
		}
	}
}
